// exhibition.js
import exhibition from '../../api/exhibition'
Page({
  data: {
    exhibition: null,
    id: null,
    loaded: [],    // 图片加载状态
    activeIndex: 0,
    loading: false,
    like:0,
    images: [
      'https://pic4.zhimg.com/v2-7d92536136452ddf676cb02780695a27_r.jpg',
      'https://pic2.zhimg.com/v2-43fa0ff6938fc02727da20812b7a571d_b.jpg',
      'https://p1.ssl.qhimg.com/t01525a62446d20148b.jpg',
      'https://pic2.zhimg.com/v2-693cfbb17e0b0036ad0a33be10d177c5_r.jpg',
      'https://pic4.zhimg.com/v2-3c8e39a7f444a3d157d8faec8c8b10b7_r.jpg'
    ]
  },
  onLoad: async function (options) {
    //从后端获取对应的信息
    this.data.id = options.id
    await exhibition.getExhibitionById(this.data.id).then(res=>{
      console.log(res.data)
      this.setData({
        exhibition:res.data
      })
      console.log(this.data.exhibition)
    }).catch(err=>{
      console.log(err)
    })
    await exhibition.isLike(this.data.id).then(res=>{
      console.log(res)
      if(res.code==200){
        this.setData({
          like:0
        })
      }else{
        this.setData({
          like:1
        })
      }
    }).catch(err=>{
      console.log(err)
    })
  },
  onPullDownRefresh() {
    // 刷新数据
    exhibition.getExhibitionById(this.data.id).then(res=>{
      console.log(res.data)
      this.setData({
        exhibition:res.data
      })
      console.log(this.data.exhibition)
    }).catch(err=>{
      console.log(err)
    })
  },
  goToArtwork: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../artwork/artwork?id=' + id
    })
  },
  onSwiperChange(e) {
    this.setData({
      activeIndex: e.detail.current
    });
  },

  imageLoaded(e) {
    const index = e.currentTarget.dataset.index;
    const loaded = this.data.loaded;
    loaded[index] = true;
    
    this.setData({
      loaded,
      loading: false
    });
  },

  imageError(e) {
    console.error('图片加载失败', e.detail.errMsg);
    const index = e.currentTarget.dataset.index;
    const loaded = this.data.loaded;
    loaded[index] = true; // 即使失败也标记，避免一直显示加载动画
    this.setData({ loaded });
  },
  //进行点赞操作
  handleChildReservation(){
    console.log("进行点赞操作主")
    console.log(this.data.like)
    if(this.data.like==0){
      exhibition.likeExhibition(this.data.exhibition.id).then(res=>{
        console.log(res)
        this.setData({
          like:1
        })
      }).catch(err=>{
        console.log(err)
      })
    }else{
      //进行点赞取消
      exhibition.cancelLike(this.data.exhibition.id).then(res=>{
        console.log(res)
        this.setData({
          like:0
        })
      }).catch(err=>{
        console.log(err)
      })
    }
  }
})
